package com.ayu.a.practice02;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 徐林玉
 * @Description FirstUniqCharPrac
 * @Date 2022/11/14 14:14
 * @Version 1.0
 */
public class FirstUniqCharPrac {

    public static void main(String[] args) {

        String s = "iloveyoui";

        int i = firstUniqChar(s);

        System.out.println("i = " + i);


    }


    public static int firstUniqChar(String s) {

        int res = -1;

        char[] c = s.toCharArray();

        Map<Character, Integer> map = new HashMap<>();

        char[] tar = new char[c.length];

        for (int i = 0; i < c.length; i++) {

            Integer index = map.get(c[i]);


            if (index == null) {

                map.put(c[i], i);

            } else {

                tar[index] = 1;

                tar[i] = 1;


            }


        }


        for (int i = 0; i < tar.length; i++) {


            if (tar[i] == 0) {

                res = i;
                break;
            }
        }

        return res;


    }


}
